import * as user from '@/api/user';
import * as types from '../action-types';
import { setLocal, getLocal } from '@/utils/local';//token存储在本地会话中
import router from '@/router'
import per from '@/router/per.js'

const filterRouter = (authList)=>{//过滤路由的方法
    //conosle.log(authList)//后端返回的数据中auth属性是进行权限鉴定的 
      let auths = authList.map(auth=>auth.auth);//获取到auths的名称进行筛选
      function filter(routes){
          return routes.filter(route=>{
              if(auths.includes(route.meta.auth)){//auths匹配上的就会有相对应的菜单
                  if(route.children){//如果有娃儿的话就继续匹配
                      route.children = filter(route.children)
                  }
                  return route;
              }
          })
      }
      return filter(per)
  }

export default {
    state:{
        userInfo: {}, // 用户信息
        hasPermission: false, // 代表用户权限
        menuPermission:false,//代表着菜单权限
    },
    mutations: {
        [types.SET_USER] (state, userInfo) {
            state.userInfo = userInfo;
            if (userInfo && userInfo.token) {
                setLocal('token', userInfo.token)
            } else {
                localStorage.clear('token')
            }
        },
        [types.SET_PERMISSION](state, has) {
            state.hasPermission = has;
        },
        [types.SET_MENU_PERMISSION](state, has) {
            state.menuPermission = has;
        }
    },
    actions: {
        [types.SET_USER]({ commit }, { payload, permission }) {
            commit(types.SET_USER, payload);
            commit(types.SET_PERMISSION, permission)
        },
        async [types.USER_LOGIN]({ commit, dispatch }, payload) {
            try {
                let result = await user.login(payload);
                dispatch(types.SET_USER,{payload:result.data,permission:true})
            } catch (e) {
                return Promise.reject(e);
            }
        },
        async [types.USER_VALIDATE]({dispatch}) {
            // 如果没token 就不用发请求了 肯定没登录
            if(!getLocal('token')) return false;
            try{
                let result =  await user.validate();
                dispatch(types.SET_USER,{payload:result.data,permission:true})
                return true;
            }catch(e){  
                dispatch(types.SET_USER,{payload:{},permission:false});
                return false;
            }
        },
        async [types.USER_LOGOUT]({ dispatch }) {
            dispatch(types.SET_USER,{payload:{},permission:false});
        },
        async [types.ADD_ROUTE]({commit, state}) {
            let authList = state.userInfo.authList; 
            if(authList) {
                let routes = filterRouter(authList); 
                let route = router.options.routes.find(item=>item.path === '/manager');
                route.children = routes; // 给manager添加儿子路由
                router.addRoutes([route]); // 动态添加进去
                commit(types.SET_MENU_PERMISSION,true); // 权限设置完毕
            }else{
                commit(types.SET_MENU_PERMISSION,true); // 权限设置完毕
            }
        }
    }
}